package leetcode;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Ghost
 * Date: 14-6-13
 * Time: 下午8:18
 * To change this template use File | Settings | File Templates.
 */
public class Integer_to_Roman {
    public String intToRoman(int num) {
        int len = 0;
        int tmp = num;
        for(;tmp!=0;len++,tmp/=10);
        int[] Num = new int[len];
        for(int i=0;num!=0;num/=10,i++)
            Num[i] = num%10;
        String rst = "";
        for(int i=Num.length-1;i>=0;i--){
            if(i==3){
                if(Num[i]<=3){
                    for(int j=0;j<Num[i];j++)
                        rst+="M";
                }
            }
            else if(i==2){
                if(Num[i]<=3)
                    for(int j=0;j<Num[i];j++)
                        rst+="C";
                else if(Num[i]==4)
                    rst+="CD";
                else if(Num[i]>=5 && Num[i]<9){
                    rst+="D";
                    for(int j=0;j<Num[i]-5;j++)
                        rst+="C";
                }
                else
                    rst+="CM";
            }
            else if(i==1){
                if(Num[i]<=3)
                    for(int j=0;j<Num[i];j++)
                        rst+="X";
                else if(Num[i]==4)
                    rst+="XL";
                else if(Num[i]>=5 && Num[i]<9){
                    rst+="L";
                    for(int j=0;j<Num[i]-5;j++)
                        rst+="X";
                }
                else
                    rst+="XC";
            }
            else{
                if(Num[i]<=3)
                    for(int j=0;j<Num[i];j++)
                        rst+="I";
                else if(Num[i]==4)
                    rst+="IV";
                else if(Num[i]>=5 && Num[i]<9){
                    rst+="V";
                    for(int j=0;j<Num[i]-5;j++)
                        rst+="I";
                }
                else
                    rst+="IX";
            }
        }
        return rst;
    }
    public static void main(String[] args){
        Integer_to_Roman integer_to_roman = new Integer_to_Roman();
        System.out.println(integer_to_roman.intToRoman(9));
    }
}
